﻿$(function () {

    $.datepicker.setDefaults($.datepicker.regional["es"]);

    $("#_initialDate").datepicker({
        showButtonPanel: true,
        changeMonth: true,
        changeYear: true,
        yearRange: "-100:+10",
        onSelect: function (dateText, inst) {
            var date = $(this).datepicker('getDate'),
            day = date.getDate(),
            month = date.getMonth() + 1,
            year = date.getFullYear();
            $("#InitialDate").val(month + "-" + day + "-" + year)
        }
    });

    $("#DollarPrice").live("change", function () {
        if (this.value.indexOf(",") > -1) {
            this.value = this.value.replace(",", ".");
            $(this).trigger('change');
            return false;
        }
        try {
            var newValue = Math.round((this.value * 100)) / 100;
            if (this.value != newValue) {
                this.value = newValue;
                $(this).trigger('change');
                return false;
            }

        }
        catch (err) {
            this.value = "";
        }
    });

    Custom.init();
});

function beginSubmit(content) {
    $("#ErrorMessage").html("");
    if (validateForm()) {
        $("#Save").attr("disabled", true);
        return true;
    }
    return false;
}

function submitCompleted(content) {
    var response;

    response = content.get_object().Data;

    if (response) {
        location.reload();
    }
    else {
        $("#Save").removeAttr("disabled");
        alert("error");
    }
}

function validateForm() {
    if (DoValidations(true)) {
        return true;
    }
    return false;
}

function subtmitError(content) {
    alert(content);
}

function LoadPickers() {

    // initialise the "Select date" link
    $('#date-pick-InitialDate')
	.datePicker(
    // associate the link with a date picker
		{
		createButton: false,
		startDate: '01/01/1900',
		endDate: '31/12/2020'
})
        .bind(
    // when the link is clicked display the date picker
		'click',
		function () {
		    updateTextBox($(this).dpGetSelected()[0]);
		    $(this).dpDisplay();
		    return false;
		})
        .bind(
    // when a date is selected update the SELECTs
		'dateSelected',
		function (e, selectedDate, $td, state) {
		    updateTextBox(selectedDate);
		}
	).bind(
		'dpClosed',
		function (e, selected) {
		    updateTextBox(selected[0]);
		}
	);

    var updateTextBox = function (selectedDate) {
        if (selectedDate != null) {
            var selectedDate = new Date(selectedDate);
            $("#InitialDate").val(selectedDate.getDate() + "/" + (selectedDate.getMonth() + 1) + "/" + selectedDate.getFullYear());

        }
    }
}

function preventBackspace(e) {
    var evt = e || window.event;
    if (evt) {
        var keyCode = evt.charCode || evt.keyCode;
        if (keyCode === 8) {
            if (evt.preventDefault) {
                evt.preventDefault();
            } else {
                evt.returnValue = false;
            }
        }
    }
}